package com.example.system.information.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.example.system.information.global.ConstantInfo;
import com.example.system.information.utils.CommonUtils;
import com.example.system.information.utils.LogLocationUtils;

import static android.os.BatteryManager.BATTERY_STATUS_CHARGING;
import static android.os.BatteryManager.EXTRA_STATUS;

/**
 * 接收断电/充电广播
 */
public class BatteryReceiver extends BroadcastReceiver {

    private static final String TAG = BatteryReceiver.class.getSimpleName();

    // 接收到广播后，则自动调用该方法
    @Override
    public void onReceive(Context context, Intent intent) {

        int status = intent.getIntExtra(EXTRA_STATUS, -1);//状态

        switch (status){
            case BATTERY_STATUS_CHARGING://充电中
                ConstantInfo.IsBattery = true;

                if(ConstantInfo.IsRestart){
                    LogLocationUtils.writeLog(TAG,"BatteryReceiver：断电之后重新充电，需要重启");
                    System.exit(0);
                }else {
                    LogLocationUtils.writeLog(TAG,"BatteryReceiver：连续充电，不需要重启");
                }

                ConstantInfo.IsRestart = false ;
                break;
            default://非充电
                LogLocationUtils.writeLog(TAG,"BatteryReceiver：进行了断电操作，清除缓存数据");

                ConstantInfo.IsBattery = false;
                ConstantInfo.IsRestart = true ;

                // 清空应用缓存
                CommonUtils.clearData();
                break;
        }
    }
}

